package com.bodacn.keypoint;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static android.os.Environment.getDataDirectory;
import static android.os.Environment.getExternalStorageDirectory;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    public static final int getParamFragmentA = 701;
    public static final int getParamFragmentB = 702;
    public static final int getParamFragmentC = 703;
    public static final int getParamFragmentD = 704;
    public static final int getParamFragmentE = 705;
    public static final int getParamFragmentF = 706;
    public static final int msgFragmentA = 801;
    public static final int msgFragmentB=802;
    public static final int msgFragmentC=803;
    public static final int msgFragmentD=804;
    public static final int msgFragmentE=805;
    public static final int msgFragmentF=806;
    //全局变量
    String SaveFilePath="",SaveDBFolder="/DB";
    public static final String DB_Name ="bdmes.db";
    //全局变量
    String STORE_NAME = "Settings";
    //存放当前Fragment
    private Fragment mContent;
    private FragmentManager fragmentManager=getSupportFragmentManager();
    class FragmentStruct{
        Fragment frgmt;
        String ident;
    }
    private final List<FragmentStruct> mFragments=new ArrayList<FragmentStruct>();//fragment列表;
    private int curFragmentIndex = 0;//当前显示页面;
    //服务器地址
    String language="";
    String serviceUrl="http://10.10.60.2/BDCOMM";
    String serviceUrlExt  = "/BDService.asmx?wsdl";
    String curLoginUserNo,curLoginUser,curFactory, curDepartNo,curDepart,CPUID;//操作员所属部门等信息;
    int curOperateTime =600;//工作分钟
    int curPlaceCount=60;//工位数;
    //界面按钮;
    TextView txt_titleInv,txt_funcA,txt_funcB, txt_funcC, txt_funcD, txt_funcE,txt_funcF;
    TextView txt_funcG,txt_funcH, txt_funcI, txt_funcJ, txt_funcK,txt_funcL;
    Button btn_LoginUser, btn_LoginClose;
    ImageView titleImgView;
    Toast curToast;
    JSONArray curUserPower;
    //用于将Activity中的参数传给Fragment
    private Map<String,Object> mParams=new HashMap<String,Object>();


    //---------------------------接口申明及调用------------------------开始;
    //A:定义一个传参接口;
    public  interface OnTransParamsListenerA{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerA transParamsListenerA;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerA(OnTransParamsListenerA transParamsListener){
        this.transParamsListenerA=transParamsListener;
    }
    //B:定义一个传参接口;
    public  interface OnTransParamsListenerB{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerB transParamsListenerB;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerB(OnTransParamsListenerB transParamsListener){
        this.transParamsListenerB=transParamsListener;
    }
    //C:定义一个传参接口;
    public  interface OnTransParamsListenerC{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerC transParamsListenerC;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerC(OnTransParamsListenerC transParamsListener){
        this.transParamsListenerC=transParamsListener;
    }
    //D:定义一个传参接口;
    public  interface OnTransParamsListenerD{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerD transParamsListenerD;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerD(OnTransParamsListenerD transParamsListener){
        this.transParamsListenerD=transParamsListener;
    }
    //E:定义一个传参接口;
    public  interface OnTransParamsListenerE{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerE transParamsListenerE;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerE(OnTransParamsListenerE transParamsListener){
        this.transParamsListenerE=transParamsListener;
    }
    //F定义一个传参接口;
    public  interface OnTransParamsListenerF{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerF transParamsListenerF;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerF(OnTransParamsListenerF transParamsListener){
        this.transParamsListenerF=transParamsListener;
    }
    //---------------------\\\\\\各Fragment回调实现//////-------------------------;
    public  interface OnTransParamsListenerG{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerG transParamsListenerG;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerG(OnTransParamsListenerG transParamsListener){
        this.transParamsListenerG=transParamsListener;
    }
    public  interface OnTransParamsListenerH{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerH transParamsListenerH;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerH(OnTransParamsListenerH transParamsListener){
        this.transParamsListenerH=transParamsListener;
    }
    public  interface OnTransParamsListenerI{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerI transParamsListenerI;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerI(OnTransParamsListenerI transParamsListener){
        this.transParamsListenerI=transParamsListener;
    }
    public  interface OnTransParamsListenerJ{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerJ transParamsListenerJ;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerJ(OnTransParamsListenerJ transParamsListener){
        this.transParamsListenerJ=transParamsListener;
    }
    public  interface OnTransParamsListenerK{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerK transParamsListenerK;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerK(OnTransParamsListenerK transParamsListener){
        this.transParamsListenerK=transParamsListener;
    }
    public  interface OnTransParamsListenerL{
        // 里面传个值@param Map<String,Object>适应多种类型;
        public void TransParams(Map<String, Object> mParams);
    }
    //2:对外公开的transParamsListener事件,配合接口及对外公布的事件;
    private OnTransParamsListenerL transParamsListenerL;
    //3:写一个对外公开的方法setTransParamsListener,供Fragment加接口监使用;
    public void setTransParamsListenerL(OnTransParamsListenerL transParamsListener){
        this.transParamsListenerL=transParamsListener;
    }
    //核心设置的代码
    protected void switchLanguage() {
        //读取配置文件-设置语言
        SharedPreferences settings = getSharedPreferences(STORE_NAME, MODE_PRIVATE);
        serviceUrl=settings.getString("serviceUrl",serviceUrl);
        language=settings.getString("language",language);
        //获取资源
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        switch (language) {
            case "zh":
                config.locale = new Locale("zh","");
                resources.updateConfiguration(config, dm);
                break;
            case "en":
                config.locale = new Locale("en","");
                resources.updateConfiguration(config, dm);
                break;
            case "km":
                config.locale = new Locale("km","");
                resources.updateConfiguration(config, dm);
                break;
            default:
                config.locale = new Locale("zh","");
                resources.updateConfiguration(config, dm);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置语言;
        switchLanguage();
        //创建目录;
        try{
            //取存储路径;
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                //SD卡准备好的话，优先存储;
                SaveFilePath = getExternalStorageDirectory().toString();
                Log.d("msg","用存储卡:"+SaveFilePath);
            }
            else{
                //系统内部Flash;
                SaveFilePath =this.getFilesDir().toString();
                Log.d("msg","用系统目录:"+SaveFilePath);
            }
            //组织并判断是否需要创建Root目录;
            SaveFilePath = SaveFilePath +"/BDMES";
            Log.d("msg","存储目录为:"+SaveFilePath);
            //判断是否需要创建BDMES根目录;
            if (isExternalStorageWritable()==true) {
                //主目录;
                File file = new File(SaveFilePath);
                if (!file.isDirectory()) file.mkdir();
                Log.d("msg","目录创建成功:"+ SaveFilePath );
                //数据库目录
                file = new File(SaveFilePath +SaveDBFolder);
                if (!file.isDirectory()) file.mkdir();
                Log.d("msg","目录创建成功:"+ SaveFilePath +SaveDBFolder);
            }
            //设置页面;
            setContentView(R.layout.activity_main);
            //获取登陆参数
            Bundle paramBundle = this.getIntent().getExtras();
            curLoginUserNo=paramBundle.getString("LoginUserNo");
            curLoginUser=paramBundle.getString("LoginUser");
            curFactory=paramBundle.getString("CurFactory");
            curDepartNo=paramBundle.getString("CurDepartNO");
            curDepart=paramBundle.getString("CurDepart");
            curPlaceCount=paramBundle.getInt("PlaceCount");
            curOperateTime=paramBundle.getInt("OperateTime");
            serviceUrl=paramBundle.getString("serviceUrl");
            serviceUrlExt=paramBundle.getString("serviceUrlExt");
            curUserPower=new JSONArray(paramBundle.getString("UserPower"));
            CPUID=paramBundle.getString("CPUID");
            language=paramBundle.getString("language");
            System.out.println(paramBundle);
            //初始化按键，并将页面加载到fragment列表中;
            initView();
            //显示登陆员工;
            btn_LoginUser.setText(curLoginUser);
            System.out.println("onCreate ------------------------setText---------2.3");
            mParams.clear();
            mParams.put("ParamComm", "LoginParam");
            mParams.put("LoginUserNo", curLoginUserNo);
            mParams.put("LoginUser", curLoginUser);
            mParams.put("DepartNo", curDepartNo);
            mParams.put("Depart", curDepart);
            mParams.put("Factory", curFactory);
            mParams.put("CPUID",CPUID);
            mParams.put("PlaceCount",curPlaceCount);
            mParams.put("OperateTime",curOperateTime);
            mParams.put("ServiceUrl",serviceUrl+serviceUrlExt);
            mParams.put("language",language);

        } catch (Exception e){
            Toast.makeText(this,String.valueOf("出错了，"+e.getMessage()),Toast.LENGTH_LONG).show();
        }
        //全屏
        hideBottomUIMenu();
        if (curUserPower!=null) {
            System.out.println("curUserPower:"+curUserPower);
            txt_funcA.setVisibility(View.GONE);
            txt_funcB.setVisibility(View.GONE);
            txt_funcC.setVisibility(View.GONE);
            txt_funcD.setVisibility(View.GONE);
            txt_funcE.setVisibility(View.GONE);
            txt_funcF.setVisibility(View.GONE);
            txt_funcG.setVisibility(View.GONE);
            txt_funcH.setVisibility(View.GONE);
            txt_funcI.setVisibility(View.GONE);
            txt_funcJ.setVisibility(View.GONE);
            txt_funcK.setVisibility(View.GONE);
            txt_funcL.setVisibility(View.GONE);

            System.out.println("onCreate:A1");
            if (GetUserFuncPower("FuncA") == 1) {
                txt_funcA.setVisibility(View.VISIBLE);
                AddFragment("FragmentA");
            }
            if (GetUserFuncPower("FuncB") == 1) {
                txt_funcB.setVisibility(View.VISIBLE);
                AddFragment("FragmentB");
            }
            if (GetUserFuncPower("FuncC") == 1) {
                txt_funcC.setVisibility(View.VISIBLE);
                AddFragment("FragmentC");
            }
            if (GetUserFuncPower("FuncD") == 1) {
                txt_funcD.setVisibility(View.VISIBLE);
                AddFragment("FragmentD");
            }
            if (GetUserFuncPower("FuncE") == 1) {
                txt_funcE.setVisibility(View.VISIBLE);
                AddFragment("FragmentE");
            }
            if (GetUserFuncPower("FuncF") == 1) {
                txt_funcF.setVisibility(View.VISIBLE);
                AddFragment("FragmentF");
            }
            if (GetUserFuncPower("FuncG") == 1) {
                txt_funcG.setVisibility(View.VISIBLE);
                AddFragment("FragmentG");
            }
            if (GetUserFuncPower("FuncH") == 1) {
                txt_funcH.setVisibility(View.VISIBLE);
                AddFragment("FragmentH");
            }
            if (GetUserFuncPower("FuncI") == 1) {
                txt_funcI.setVisibility(View.VISIBLE);
                AddFragment("FragmentI");
            }
            if (GetUserFuncPower("FuncJ") == 1) {
                txt_funcJ.setVisibility(View.VISIBLE);
                AddFragment("FragmentJ");
            }
            if (GetUserFuncPower("FuncK") == 1) {
                txt_funcK.setVisibility(View.VISIBLE);
                AddFragment("FragmentK");
            }
            if (GetUserFuncPower("FuncL") == 1) {
                txt_funcL.setVisibility(View.VISIBLE);
                AddFragment("FragmentL");
            }
        }
        //设置到第一个Fragment;
        loadDefaultFragment();
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d("系统提示","onResume");
        hideBottomUIMenu();
        System.out.println("onResume--Start");
        /*增减功能时这里要同步修改*/
        if (curFragmentIndex>-1){
            System.out.println("curFragmentIndex:"+curFragmentIndex);
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentA")) {Selected(txt_funcA);sendParamFragmentA();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentB")) {Selected(txt_funcB);sendParamFragmentB();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentC")) {Selected(txt_funcC);sendParamFragmentC();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentD")) {Selected(txt_funcD);sendParamFragmentD();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentE")) {Selected(txt_funcE);sendParamFragmentE();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentF")) {Selected(txt_funcF);sendParamFragmentF();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentG")) {Selected(txt_funcG);sendParamFragmentG();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentH")) {Selected(txt_funcH);sendParamFragmentH();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentI")) {Selected(txt_funcI);sendParamFragmentI();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentJ")) {Selected(txt_funcJ);sendParamFragmentJ();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentK")) {Selected(txt_funcK);sendParamFragmentK();}
            if (mFragments.get(curFragmentIndex).ident.equals("FragmentL")) {Selected(txt_funcL);sendParamFragmentL();}

        }

    }

    //获取权限,默认都有;
    private int GetUserFuncPower(String paramFunc){
        if (curUserPower.length()>0)
        {
            for (int i=0;i<curUserPower.length();i++){
                if (curUserPower.optJSONObject(i).has(paramFunc)==true)
                {
                    try {
                        return curUserPower.optJSONObject(i).getInt(paramFunc);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            return 0;
        }
        return 0;
    }
    @Override
    protected void onStart() {
        super.onStart();
        /*发参数到第一个Fragment*/
        sendParamFragmentA();
        hideBottomUIMenu();
    }

    //加载需要用到的Fragment
    private boolean AddFragment(String paramFrgmtName){
        //创建Fragment;
        FragmentStruct tmpFrgmtStruct=new FragmentStruct();
        try {
            Log.d("AddFragment",paramFrgmtName+"--Start");
            tmpFrgmtStruct.frgmt=(Fragment) Class.forName(getPackageName()+"."+paramFrgmtName).newInstance();
            tmpFrgmtStruct.ident=paramFrgmtName;
            mFragments.add(tmpFrgmtStruct);
            Log.d("AddFragment",paramFrgmtName+"--Over");
            return true;
        }catch (ClassNotFoundException err){
            err.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return  false;//未成功加载
    }

    //根据顺序预加载Fragment
    private void loadDefaultFragment(){
        curFragmentIndex=-1;
        if (mFragments.size()>0) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Log.d("msg", "Add Fragment------------------------《预加载》");
            for (int i = mFragments.size() - 1; i >= 0; i--) {
                curFragmentIndex = i;
                Log.d("预加载", mFragments.get(curFragmentIndex).ident);
                mContent = mFragments.get(curFragmentIndex).frgmt;
                fragmentTransaction.add(R.id.mFragmentContainer, mContent);
                fragmentTransaction.hide(mContent);
            }
            //提交修改并显示;
            fragmentTransaction.show(mContent);
            fragmentTransaction.commit();

        }
    }
    //Fragment切换
    public void switchFragment(Fragment from, String toFrgmtName) {
        Fragment to=null;
        for (int i=0;i<mFragments.size();i++)
        {
            if (mFragments.get(i).ident.equals(toFrgmtName)==true)
            {
                to=mFragments.get(i).frgmt;
                curFragmentIndex=i;
                break;
            }
        }
        if ((mContent != to)&&(to!=null)) {
            mContent = to;
            FragmentTransaction transaction = fragmentManager.beginTransaction().setCustomAnimations(
                    android.R.anim.slide_in_left,android.R.anim.slide_out_right);
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(from).add(R.id.mFragmentContainer, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }

    }

    private void initView()
    {
        txt_titleInv=(TextView) findViewById(R.id.txt_titleInv);
        //顶端图标;
        titleImgView=(ImageView) findViewById(R.id.titleImgView);
        titleImgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //将临时文件复制到SD卡;
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    Log.d("MSG-getDatabasePath",getDatabasePath(DB_Name).toString());
                    Log.d("MSG-getDataDirectory",getDataDirectory().toString());
                    Log.d("MSG-StorageDirectory", getExternalStorageDirectory().toString());
                    //将数据复制到SD卡;
                    copyFile(getDatabasePath(DB_Name).toString(),SaveFilePath+SaveDBFolder+"/"+DB_Name);
                    copyFolder(getCacheDir().toString(),SaveFilePath);
                }

            }
        });
        titleImgView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog alertDialog = new AlertDialog.Builder(v.getContext())
                        .setTitle("系统提示").setMessage("您确定要从临时数据复制到外部存储中吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                //将临时文件复制到SD卡;
                                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                                    Log.d("MSG-getDatabasePath",getDatabasePath(DB_Name).toString());
                                    Log.d("MSG-getDataDirectory",getDataDirectory().toString());
                                    Log.d("MSG-StorageDirectory", getExternalStorageDirectory().toString());
                                    //将SD卡数据复制到数据目录;
                                    //copyFile(SaveFilePath+SaveDBFolder+"/"+DB_Name,getDatabasePath(DB_Name).toString());
                                    copyFile(getDatabasePath(DB_Name).toString(),SaveFilePath+SaveDBFolder+"/"+DB_Name);
                                }
                            }

                        }).setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        return;
                                    }
                                }).create(); // 创建对话框
                alertDialog.show(); // 显示对话框
                return false;
            }
        });
        //下边栏按钮
        txt_funcA= (TextView) findViewById(R.id.txt_funcA);
        txt_funcB= (TextView) findViewById(R.id.txt_funcB);
        txt_funcC= (TextView) findViewById(R.id.txt_funcC);
        txt_funcD= (TextView) findViewById(R.id.txt_funcD);
        txt_funcE= (TextView) findViewById(R.id.txt_funcE);
        txt_funcF= (TextView) findViewById(R.id.txt_funcF);
        txt_funcG= (TextView) findViewById(R.id.txt_funcG);
        txt_funcH= (TextView) findViewById(R.id.txt_funcH);
        txt_funcI= (TextView) findViewById(R.id.txt_funcI);
        txt_funcJ= (TextView) findViewById(R.id.txt_funcJ);
        txt_funcK= (TextView) findViewById(R.id.txt_funcK);
        txt_funcL= (TextView) findViewById(R.id.txt_funcL);

        //设置监听;
        txt_funcA.setOnClickListener(this);
        txt_funcB.setOnClickListener(this);
        txt_funcC.setOnClickListener(this);
        txt_funcD.setOnClickListener(this);
        txt_funcE.setOnClickListener(this);
        txt_funcF.setOnClickListener(this);
        txt_funcG.setOnClickListener(this);
        txt_funcH.setOnClickListener(this);
        txt_funcI.setOnClickListener(this);
        txt_funcJ.setOnClickListener(this);
        txt_funcK.setOnClickListener(this);
        txt_funcL.setOnClickListener(this);
        //退出
        btn_LoginUser=(Button)findViewById(R.id.btn_LoginUser);
        btn_LoginClose =(Button)findViewById(R.id.btn_LoginClose);
        btn_LoginClose.setOnClickListener(this);
        //初始提醒;
        curToast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG);
        curToast.setGravity(Gravity.CENTER, 0, 0);

    }

    //传递参数到MainFragment
    private void sendParamFragmentA() {
        Log.d("SendParam", "sendParamFragmentA------------------Enter;");
        if (transParamsListenerA != null) {
            Log.d("SendParam", "sendParamFragmentA------------------1;");
            transParamsListenerA.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentA------------------2;");
        }
    }

    //传递参数到MainFragment
    private void sendParamFragmentB() {
        Log.d("SendParam", "sendParamFragmentB------------------Enter;");
        if (transParamsListenerB != null) {
            Log.d("SendParam", "sendParamFragmentB------------------1;");
            transParamsListenerB.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentB------------------2;");
        }
    }

    //传递参数到MainFragment
    private void sendParamFragmentC() {
        Log.d("SendParam", "sendParamFragmentC------------------Enter;");
        if (transParamsListenerC != null) {
            Log.d("SendParam", "sendParamFragmentC------------------1;");
            transParamsListenerC.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentC------------------2;");
        }
    }
    //传递参数到MainFragment
    private void sendParamFragmentD() {
        Log.d("SendParam", "sendParamFragmentD------------------Enter;");
        if (transParamsListenerD != null) {
            Log.d("SendParam", "sendParamFragmentD------------------1;");
            transParamsListenerD.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentD------------------2;");
        }
    }
    //传递参数到MainFragment
    private void sendParamFragmentE() {
        Log.d("SendParam", "sendParamFragmentE------------------Enter;");
        if (transParamsListenerE != null) {
            Log.d("SendParam", "sendParamFragmentE------------------1;");
            transParamsListenerE.TransParams(mParams);
            System.out.println(mParams);
            Log.d("SendParam", "sendParamFragmentE------------------2;");
        }
    }
    //传递参数到MainFragment
    private void sendParamFragmentF() {
        Log.d("SendParam", "sendParamFragmentF------------------Enter;");
        if (transParamsListenerF != null) {
            Log.d("SendParam", "sendParamFragmentF------------------1;");
            transParamsListenerF.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentF------------------2;");
        }
    }
    private void sendParamFragmentG() {
        Log.d("SendParam", "sendParamFragmentF------------------Enter;");
        if (transParamsListenerG != null) {
            Log.d("SendParam", "sendParamFragmentF------------------1;");
            transParamsListenerG.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentF------------------2;");
        }
    }
    private void sendParamFragmentH() {
        Log.d("SendParam", "sendParamFragmentF------------------Enter;");
        if (transParamsListenerH != null) {
            Log.d("SendParam", "sendParamFragmentF------------------1;");
            transParamsListenerH.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentF------------------2;");
        }
    }
    private void sendParamFragmentI() {
        Log.d("SendParam", "sendParamFragmentF------------------Enter;");
        if (transParamsListenerI!= null) {
            Log.d("SendParam", "sendParamFragmentF------------------1;");
            transParamsListenerI.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentF------------------2;");
        }
    }
    private void sendParamFragmentJ() {
        Log.d("SendParam", "sendParamFragmentF------------------Enter;");
        if (transParamsListenerJ!= null) {
            Log.d("SendParam", "sendParamFragmentF------------------1;");
            transParamsListenerJ.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentF------------------2;");
        }
    }
    private void sendParamFragmentK() {
        Log.d("SendParam", "sendParamFragmentF------------------Enter;");
        if (transParamsListenerK != null) {
            Log.d("SendParam", "sendParamFragmentF------------------1;");
            transParamsListenerK.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentF------------------2;");
        }
    }
    private void sendParamFragmentL() {
        Log.d("SendParam", "sendParamFragmentF------------------Enter;");
        if (transParamsListenerL != null) {
            Log.d("SendParam", "sendParamFragmentF------------------1;");
            transParamsListenerL.TransParams(mParams);
            Log.d("SendParam", "sendParamFragmentF------------------2;");
        }
    }
    /**
     * 定义一个Handler用于接收Fragment给Activity发出来的指令
     */
    public Handler mainHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg!=null){
                switch (msg.what) {
                    case getParamFragmentA:

                        break;
                    case getParamFragmentB:

                        break;
                    case getParamFragmentC:

                        break;
                    case getParamFragmentD:
                        //mViewPager.setCurrentItem(0,true);
                        break;
                    case getParamFragmentE:
                        //mViewPager.setCurrentItem(1,true);
                        break;
                    case getParamFragmentF:
                        //mViewPager.setCurrentItem(2,true);
                        break;
                    default:
                        break;
                }
            }
        }
    };

    //获取当前显示的Fragment
    public Fragment getVisibleFragment() {
        FragmentManager fragmentManager;
        fragmentManager = MainActivity.this.getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
        return null;
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏状态栏和底部虚拟按键
        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(flag);
        }
    }
    //重置所有文本的选中状态
    public void unSelected(){

        txt_funcA.setSelected(false);
        txt_funcA.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcB.setSelected(false);
        txt_funcB.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcC.setSelected(false);
        txt_funcC.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcD.setSelected(false);
        txt_funcD.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcE.setSelected(false);
        txt_funcE.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcF.setSelected(false);
        txt_funcF.setTextColor(getResources().getColor(R.color.text_gray));

        txt_funcG.setSelected(false);
        txt_funcG.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcH.setSelected(false);
        txt_funcH.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcI.setSelected(false);
        txt_funcI.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcJ.setSelected(false);
        txt_funcJ.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcK.setSelected(false);
        txt_funcK.setTextColor(getResources().getColor(R.color.text_gray));
        txt_funcL.setSelected(false);
        txt_funcL.setTextColor(getResources().getColor(R.color.text_gray));
    }
    public void Selected(TextView paramBtn){
        paramBtn.setSelected(true);
        paramBtn.setTextColor(getResources().getColor(R.color.text_blue));
        txt_titleInv.setText(paramBtn.getText());
    }

    // 隐藏手机键盘
    private void hideIM(View edt) {
        try {
            InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            IBinder windowToken = edt.getWindowToken();
            if (windowToken != null) {
                im.hideSoftInputFromWindow(windowToken, 0);
            }
        } catch (Exception e) {

        }
    }
    @Override
    public void onClick(View v) {
        hideIM(v);
        hideBottomUIMenu();
        switch (v.getId()){
            case R.id.txt_funcA:
                unSelected();
                Selected(txt_funcA);
                switchFragment(mContent,"FragmentA");
                sendParamFragmentA();
                break;
            case R.id.txt_funcB:
                unSelected();
                Selected(txt_funcB);
                switchFragment(mContent,"FragmentB");
                sendParamFragmentB();
                break;
            case R.id.txt_funcC:
                unSelected();
                Selected(txt_funcC);
                switchFragment(mContent,"FragmentC");
                sendParamFragmentC();
                break;
            case R.id.txt_funcD:
                unSelected();
                Selected(txt_funcD);
                switchFragment(mContent,"FragmentD");
                sendParamFragmentD();
                break;
            case R.id.txt_funcE://产量报帐
                unSelected();
                Selected(txt_funcE);
                switchFragment(mContent,"FragmentE");
                sendParamFragmentE();
                break;
            case R.id.txt_funcF://产量查询;
                unSelected();
                Selected(txt_funcF);
                switchFragment(mContent,"FragmentF");
                sendParamFragmentF();
                break;
            case R.id.btn_LoginClose:
                finish();//关闭当前Activity，返回上一级;
                break;

            case R.id.txt_funcG:
                unSelected();
                Selected(txt_funcG);
                switchFragment(mContent,"FragmentG");
                sendParamFragmentG();
                break;
            case R.id.txt_funcH:
                unSelected();
                Selected(txt_funcH);
                switchFragment(mContent,"FragmentH");
                sendParamFragmentH();
                break;
            case R.id.txt_funcI:
                unSelected();
                Selected(txt_funcI);
                switchFragment(mContent,"FragmentI");
                sendParamFragmentI();
                break;
            case R.id.txt_funcJ:
                unSelected();
                Selected(txt_funcJ);
                switchFragment(mContent,"FragmentJ");
                sendParamFragmentJ();
                break;
            case R.id.txt_funcK://产量报帐
                unSelected();
                Selected(txt_funcK);
                switchFragment(mContent,"FragmentK");
                sendParamFragmentK();
                break;
            case R.id.txt_funcL://产量查询;
                unSelected();
                Selected(txt_funcL);
                switchFragment(mContent,"FragmentL");
                sendParamFragmentL();
                break;
        }
    }

    //判断外部存储是否可读写
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("msg","MainActivity OnDestroy------------------------");
    }



    /**
     * 复制单个文件
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     * @return boolean
     */
    public void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ( (byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        }
        catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();

        }

    }

    /**
     * 复制整个文件夹内容
     * @param oldPath String 原文件路径 如：c:/fqf
     * @param newPath String 复制后路径 如：f:/fqf/ff
     * @return boolean
     */
    public void copyFolder(String oldPath, String newPath) {

        try {
            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
            File a=new File(oldPath);
            String[] file=a.list();
            File temp=null;
            for (int i = 0; i < file.length; i++) {
                if(oldPath.endsWith(File.separator)){
                    temp=new File(oldPath+file[i]);
                }
                else{
                    temp=new File(oldPath+File.separator+file[i]);
                }

                if(temp.isFile()){
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" +
                            (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len;
                    while ( (len = input.read(b)) != -1) {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if(temp.isDirectory()){//如果是子文件夹
                    copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
                }
            }
        }
        catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();

        }

    }
}
